package cc.sheji.controller;

import cc.sheji.domain.TestData;
import cc.sheji.domain.JsonResult;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@Validated
@RestController
@RequestMapping(value = "/api/v1/test")
public class TestController {


    @Value("${spring.datasource.url}")
    private String dbUrl;


    @RequestMapping()
    public JsonResult get(@RequestBody TestData testData) {

        System.out.println("updata row");

        String html = testData.getData();

        //   System.out.println("data:"+html);


        Document document = Jsoup.parse(html);

        // System.out.println("body:"+document.getElementsByTag("body"));


        Elements elementsByClass = document.getElementsByClass("scrollerInner-2PPAp2");

        //  System.out.println(elementsByClass);
        for (Element element : elementsByClass) {
            System.out.println("");
//            System.out.println("line:******");
//            System.out.println(element);

            Elements lis = element.getElementsByTag("li");

            for (int i = 0; i < lis.size(); i++) {


                Element element1 = lis.get(i);
                String id = element1.attr("id");
                System.out.println(id);

//                System.out.println("line:******");
//                System.out.println(element1.text());

//                if (i == lis.size() - 2) {
//                    System.out.println(element1);
//                }

                Elements elementsByClass1 = element1.getElementsByClass("originalLink-Azwuo9");
                Elements textElements = element1.getElementsByClass("markup-eYLPri");
//                Elements textElements = element1.getElementsByClass("messageContent-2t3eCI");

//                markup-eYLPri messageContent-2t3eCI

//                System.out.println(elementsByClass1);
//                System.out.println(textElements);

                for (int mm = 0; mm < elementsByClass1.size(); mm++) {
                    String href = elementsByClass1.get(mm).attr("href");
                      System.out.println(href);
                }


                for (int mm = 0; mm < textElements.size(); mm++) {


                    Element element2 = textElements.get(mm);
                    Elements as = element2.getElementsByTag("a");

                    Elements children = element2.children();

                    System.out.println("*********");

                    for (int k = 0; k < children.size(); k++) {
                        Element element3 = children.get(k);
                        if (element3.nodeName().equals("strong")) {

                        } else {
//                            System.out.println("text2:"+element3.text());
//                            System.out.println("name:"+element3);
                        }

                        System.out.println(element3);
                    }
                    System.out.println("*********");

                    //   System.out.println(as);

                    StringBuilder stringBuilder = new StringBuilder();
                    Elements strong = element2.getElementsByTag("strong");

                    if (strong.size() > 0) {
                        Element element3 = strong.get(0);
                        Elements spans = element3.getElementsByTag("span");
                        //  System.out.println("-----");
                        for (int m = 0; m < spans.size(); m++) {

                            Element element4 = spans.get(m);
                            String text = element4.text();
                            if (!text.contains("https://s.mj.run") && !text.isEmpty()) {
//                                if(text.equals("-")){
//                                    break;
//                                }
                                text = text.trim();
                                stringBuilder.append(text + " ");
                                // System.out.println(text);
                            }
                        }
                        // System.out.println("-----");
                    }
                    //  System.out.println(stringBuilder.toString());
                    // System.out.println(strong);


                }


                System.out.println("_____________________________");

            }
            System.out.println();
            System.out.println();
            System.out.println();
            System.out.println();
            System.out.println();

            System.out.println();
        }


        return JsonResult.Success(dbUrl);
    }

}
